(function(){
    angular.module('app').controller("agentIndexCtrl",function($scope,proxy,$stateParams,$rootScope){
        console.log(PERMISSION);
        $scope.checks = ["1"];
        $scope.radios = 2;
        $scope.select = [
            {
                name:"下拉框1"
            },
            {
                name:"下拉框2"
            },
            {
                name:"下拉框3"
            }
        ];
        $scope.searchVal = "";
        $scope.search = [
            {
                name:"下拉框查询1"
            },
            {
                name:"下拉框查询2"
            },
            {
                name:"下拉框查询3"
            }
        ];
        $scope.obj={
            'xAxis':[],
            'view':{  //曝光量
                'yAxis':[]
            },
            'click':{  //点击量
                'yAxis':[]
            },
            'clickRate':{  //点击率
                'yAxis':[]
            },
            'spend':{  //花费
                'yAxis':[]
            },
            'arrive':{  //到达
                'yAxis':[]
            },
            'filter':{},
            'sTime':getNowFormatDate()+' '+'00:00:00', //默认今日
            'eTime':getNowFormatDate()+' '+'23:59:59',
            'dataGrain':'hour',
            'allObj':{},
            'zhuzhuang':'曝光量',
            'zhuzhuangSelec':[
                {
                    key:'view',
                    name:"曝光量"
                },
                {
                    key:'click',
                    name:"点击量"
                },
                {
                    key:'clickRate',
                    name:"点击率"
                },
                {
                    key:'spend',
                    name:"点击率"
                },
                {
                    key:'arrive',
                    name:"到达"
                },
            ]
        };
        function getList() {
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/rpt/adv/summary/sum',
                data: {
                    sTime:$scope.obj.sTime,
                    eTime:$scope.obj.eTime,
                },
                success: function(res){
                    // 获得列表
                    $scope.obj.allObj= res.data.data;
                }
            })
        };

        $scope.targetList = [
            {id:"click",name:"点击量"},
            {id:"clickRate",name:"点击率"},
            {id:"view",name:"曝光量"},
            {id:"action",name:"转化"},
            {id:"actionRate",name:"转化率"},
            {id:"cpm",name:"cpm"},
            {id:"cpc",name:"cpc"},
        ];
        $scope.chartDate = {
            tsName: [],
            advertiserCost: [],
            view: [],
            click: [],
            clickRate: [],
            action: [],
            actionRate: [],
            cpm: [],
            cpc: []
        };
        $scope.selectTarget1 = function(item){
            $scope.chartOpt.data1 = $scope.chartDate[item.id];
            $scope.chartOpt.title1 = item.name;
            setChart($scope.chartOpt)
        };
        $scope.selectTarget2 = function(item){
            $scope.chartOpt.data2 = $scope.chartDate[item.id];
            $scope.chartOpt.title2 = item.name;
            setChart($scope.chartOpt)
        };
        //时间过滤
        $scope.chooseDate = function(sTime,eTime){
            $scope.obj.sTime = sTime+' '+'00:00:00';
            $scope.obj.eTime = eTime+' '+'23:59:59';
            getChartDate($scope.obj.url,$scope.obj.thTil,$scope.obj.tdTil);
        };
        function getChartDate(){
            var data = {
                sTime : $scope.obj.sTime,
                eTime : $scope.obj.eTime,
                "filter": {

                },
                "dataGrain":"hour",
                "page": "1",
                "size": "20"
            }
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/rpt/loc/time',
                data: data,
                success: function(res){

                    $scope.chartDate['tsName']=[];
                    $scope.obj.list = res.data.data;

                    var chartDate = $scope.chartDate;
                    res.data.data.forEach(function(v){
                        chartDate['tsName'].push(v['tsName']);
                        chartDate.advertiserCost.push(v.advertiserCost);
                        chartDate.view.push(v.view);
                        chartDate.click.push(v.click);
                        chartDate.clickRate.push(v.clickRate);
                        chartDate.action.push(v.action);
                        chartDate.actionRate.push(v.actionRate);
                        chartDate.cpm.push(v.cpm);
                        chartDate.cpc.push(v.cpc);
                    })
                    $scope.chartOpt = {
                        data1:chartDate.click,
                        data2:chartDate.view,
                        title1:"点击",
                        title2:"曝光",
                        xAxis:chartDate['tsName'],
                    }
                    setChart($scope.chartOpt)
                }
            })
        };
        var chart = echarts.init(document.getElementById('chart'));
        function setChart(option){

            var y1 = {},y2 = {};
            //y1.data = [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3];
            y1.data = option.data1;
            y1.min = 0;
            y1.max = fiveMultiple(Math.max.apply(0,y1.data));
            y1.min = y1.min == y1.max ? 0 : y1.min;
            y1.interval = Number(((y1.max - y1.min) / 5).toFixed(3));
            //y2.data = [2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23.0, 16.5, 12.0, 6.2];
            y2.data = option.data2;
            y2.min = 0;
            y2.max = fiveMultiple(Math.max.apply(0,y2.data));
            y2.min = y2.min == y2.max ? 0 : y2.min;
            y2.interval = Number(((y2.max - y2.min) / 5).toFixed(3));

            var option = {
                tooltip: {
                    trigger: 'axis',
                },
                legend: {
                    data:[option.title1,option.title2]
                },
                xAxis: [
                    {
                        type: 'category',
                        data: option.xAxis,
                        axisPointer: {
                            type: 'shadow'
                        }
                    }
                ],
                yAxis: [
                    {
                        type: 'value',
                        name: option.title1,
                        min: y1.min,
                        max: y1.max,
                        interval: y1.interval,
                    },
                    {
                        type: 'value',
                        name: option.title2,
                        min: y2.min,
                        max: y2.max,
                        interval: y2.interval,
                    }
                ],
                series: [
                    {
                        name:option.title1,
                        type:'bar',
                        color: ["#5ab1ef"],
                        barMaxWidth:30,//最大宽度

                        data: y1.data
                    },
                    {
                        name: option.title2,
                        type: 'line',
                        yAxisIndex: 1,
                        color: ['#2ec7c9'],
                        barMaxWidth:30,//最大宽度

                        data: y2.data
                    }
                ]
            };;
            chart.setOption(option);
        };

        getList();
        getChartDate();
    })
})();
